/***************************************************
> Copyright (C) 2024 ==RXX== All rights reserved.
> File Name: 1.c
> Author: rxx
> Mail:2909017383@qq.com 
> Created Time: 2024年12月05日 星期四 09时24分09秒
***************************************************/
#include "freexx.h"
#include <ctype.h>

//会员注册系统
void regMem(MEM *memhead)
{
	memhead = inputHeadMem(memhead);
	int flag7 = 1;
	int num4 = 0;
	while(flag7)
	{
		menu3();
		printf("请输入要办理的会员流程！！！\n");
		scanf("%d",&num4);
		switch(num4)
		{
		//新会员注册(实名制)
			case 1:
				newMem(memhead);
				break;
		//新会员登陆(实名制)
			case 2:
				logOnMem(memhead);
				break;
		//查看会员余额
			case 3:
				ownMem(memhead);
				break;
		//会员修改信息
			case 4:
				cardRefund(memhead);
				break;
		//会员充值缴费
			case 5:
			   	memSuc(memhead);
				break;
		// 会员退卡
			case 6:
				cardRefund(memhead);
				break;
		// 退出
			case 7:
				flag7 = 0;
				break;
			default:
				printf("输入错误！！！\n");
				outputMem(memhead);
				flag7 = 0;
				break;
		}
	}
//	freeSpaceMem(memhead);
}


//会员菜单
void menu3()
{
	printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("\t\t\t  欢迎来到会员界面！！！\n");
	printf("\t\t\t1 新会员注册(实名制)\n");
	printf("\t\t\t2 新会员登陆(实名制)\n");
	printf("\t\t\t3 查看会员余额\n");
	printf("\t\t\t4 会员修改信息\n");
	printf("\t\t\t5 会员充值缴费\n");
	printf("\t\t\t6 会员退卡\n");
	printf("\t\t\t7 退出\n");
	printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
//设置头节点
MEM *inputHeadMem(MEM *memhead)
{
	//给头节点申请空间
	memhead = (MEM*)malloc(sizeof(MEM));
	memset (memhead,0,sizeof(MEM));
	if(memhead == NULL)
	{
		perror("malloc");
		exit(1);
	}
	return memhead;
}
//注册新会员
MEM *newMem(MEM *memhead)
{
	assert(memhead != NULL);
	//尾节点
	MEM *end = memhead;
	while(end->next != NULL)
	{
		end = end->next; 
	}
	//给新节点申请空间
	MEM *node = (MEM*)malloc(sizeof(MEM));
	memset(node,0,sizeof(MEM));

	//注册新会员账号
	printf("输入newuser：");
	scanf("%s",node->user);
	//新会员密码
	printf("输入newpassword：");
	scanf("%s",node->password);
	//新会员姓名
	printf("输入name：");
	scanf("%s",node->name);
	//新会员手机号
	printf("输入telephone：");
	scanf("%s",node->telephone);
	//判断用户名长度，6-8位之间
	printf("%s\n",node->telephone);
	printf("%d\n", __LINE__);
	if(strlen(node->user) >= 6 && strlen(node->user) <= 8)
	{
		printf("%d\n", __LINE__);
		//判断密码长度，6-8位之间
		if(strlen(node->password) >= 6 && strlen(node->password) <= 8)
		{
			//电话号11个数字
			if(strlen(node->telephone) != 11){
				return memhead;
			}
#if 1
			for(int i = 0; i < 11; i++){
				if(!isdigit(node->telephone[i])){
					return memhead;
				}
			}
#endif
			memSuc(memhead);
			end->next = node;
			node->next = NULL;
		}
	}
	else
	{
		printf("输入格式不正确！！！\n 注册失败请重新输入！！!\n");
	}
	return memhead;
}
//办理会员的条件
int memSuc(MEM *memhead)
{
	int	money = 0;
	printf("请输入充值的金额：1、50\n2、100\n3、150\n");
	scanf("%d",&money);
	switch(money)
	{
		case 1:
			printf("充值50送2元，办理会员成功！！！\n");
			money = 52;
			break;
		case 2:
			printf("充值100送5元，办理会员成功！！！\n");
			money = 105;
			break;
		case 3:
			printf("充值150送10元，办理会员成功！！！\n");
			money = 160;
			break;
		default:
			printf("input error\n");
			break;
	}
	return money;

}
//输出链表
MEM *outputMem(MEM *memhead)
{
	MEM *end = memhead->next;//尾节点
	while(end != NULL)
	{
		printf("user:%s\n",end->user);
		printf("password:%s\n",end->password);
		end = end->next;
	}
	return memhead;
}
//登陆用户
int logOnMem(MEM *memhead)
{
	MEM *end = memhead->next;//尾节点
	char user[20];
	char password[20];
	int isOk = 0;
	printf("输入user：");
	scanf("%s",user);
	printf("输入password：");
	scanf("%s",password);
	while(end != NULL)
	{
	   	if( strcmp(end->user,user) == 0 && 
		strcmp(end->password,password) == 0 )
		{
			printf("登陆成功！\n");
			isOk = 1;//对比成功
			break;
			
		}
		end = end->next;
	}
	if(end == NULL)
	{
		printf("登陆失败！\n");
		isOk = 0;
	}//对比不成功
	return isOk;
}
//会员查看自己的余额
void ownMem(MEM *memhead)
{
	MEM *end = memhead->next;//尾节点
	char user[20];
	char password[20];
	int isOk = 0;
	printf("输入user：");
	scanf("%s",user);
	printf("输入password：");
	scanf("%s",password);
	while(end != NULL)
	{
	   	if( strcmp(end->user,user) == 0 && 
		strcmp(end->password,password) == 0 )
		{
			printf("%d",end->money);
			isOk = 1;//对比成功
			break;
			
		}
		end = end->next;
	}
	if(end == NULL)
	{
		printf("登陆失败！\n");
		isOk = 0;
	}//对比不成功
}

//修改节点的值
MEM *modifyMem(MEM *memhead,char *value)
{
	MEM *end = memhead->next;//尾节点
	char str[20] ;
	char str1[20];
	char str2[20];
	while(end != NULL)
	{
		if( (strcmp(end->user ,value)) == 0)
		{
			printf("输入要修改的密码：");
			scanf("%s",str);
			strcpy(end->password,str);
			printf("输入要修改的姓名：");
			scanf("%s",str1);
			strcpy(end->name,str1);
			printf("输入要修改的电话号码：");
			scanf("%s",str2);
			strcpy(end->telephone,str2);
			printf("修改成功！\n");
			return 0;
		}
		end = end->next;
	}
	if(end == NULL)
	{
		printf("没有该会员！！！\n");
	}
	return memhead;
}
//退卡
MEM* cardRefund(MEM *memhead)
{
	MEM *end = memhead->next;//尾节点
	char val[20];
	char passwordnow[20];
	int nowmoney = 0;
	int flag6 = 1;
	printf("请输入要退卡的卡号：");
	scanf("%s",val);
	printf("请输入要退卡的密码：");
	scanf("%s",passwordnow);
	while(end != NULL)
	{
		if( strcmp(end->user,val) == 0 && 
				strcmp(end->password,passwordnow) == 0 )
		{
			printf("对比成功！\n");
			printf("%d\n",end->money);
			printf("请确认要退款的金额：");
			scanf("%d",&nowmoney);
			while(flag6)
			{
				if(nowmoney == end->money)
				{
					printf("退款成功请注意查收！！！\n");
					deleteUser(memhead,val);
					flag6 = 0;
				}else 
				{
					printf("金额收入错误请重新输入!!!\n");
				}
			}
			end = end->next;
		}
	}
	if(end == NULL)
	{
			printf("密码或账号错误！！！\n");
	}
	return memhead;
}
//退卡的删除节点
MEM *deleteUser(MEM *memhead,char* val)
{
	MEM *bef = memhead;
	MEM *end = NULL;
	MEM *temp = NULL;
	
	end = memhead->next;//尾节点
	
	while(end!=NULL)
	{
		if( end->user == val)//找到end->data等于val删除
		{
			temp = end;
			break;
		}
		bef = end;
		end = end->next;//让链表指向下一个值
	}
	//未找到删除的节点
	if(end == NULL)
	{
		printf("not find\n");
		memhead = NULL;
	}
	else
	{
		bef->next = temp->next;
		free(temp);
		temp = NULL;
	}
	return memhead;
}
//释放空间
void freeSpaceMem(MEM *memhead)
{
	//令tmp = memhead
	MEM *tmp = memhead;
	//tmp不等于0的时候进入循环
	while(tmp != NULL)
	{
		//让memhead等于tmp
		memhead = tmp;
		//让tmp等于tmp的下一个
		tmp = tmp->next;
		//释放memhead
		free(memhead);
	}
}
